package cn.ekuma.epos.analysis.front;

import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.db.table.erp.subject.I_SubjectProduct;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.rabbit.spi.I_ModelPart;

import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.LinkQBFParameter;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.format.Formats;

import com.openbravo.pos.base.*;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.JCustomerFinder;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.navigator.JCustomerNavigator;
import com.openbravo.pos.panels.navigator.JOperationProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JSubjectNavigator;

import java.awt.CardLayout;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class JPassengerFlowAnalysisPanel extends javax.swing.JPanel implements JPanelView, BeanFactoryApp,EditorCreator, TimeCycleChangedListener,I_ModelPart<ERP_UI_Swing_Model>{
    private I_DataLogicERP  dlSales;
    private I_DataLogicCustomers dlCustomers;
    private I_DataLogicSystem dlSystem;
    private AnalysisModelManager manager;
    private JPassengerFlowHistoryView view;
    private JPassengerFlowTableView table;
    private CustomerInfo customerInfo;
    private String showPanel;
	private QuickTimerFilter timerFilter;
	Object[] beforeFilterParas;
	private JProductFinder productFinder;
	
    LinkQBFParameter subjectLinkParameter=new LinkQBFParameter(I_SubjectProduct.TABLENAME,I_SubjectProduct.PRODUCT);
    
    /** Creates new form JProductAnalysisPanel */
    public JPassengerFlowAnalysisPanel() {
    	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.CardLayout());
        add(jPanel1, java.awt.BorderLayout.CENTER);
    	
    	panel = new JPanel();
    	add(panel, BorderLayout.NORTH);
    	panel.setLayout(new BorderLayout(0, 0));
    	
    	        jToolBar1 = new javax.swing.JToolBar();
    	        panel.add(jToolBar1, BorderLayout.NORTH);
    	        
    	        timerFilter=new QuickTimerFilter();
    	        panel.add(timerFilter,BorderLayout.CENTER);
    	        timerFilter.addTimeCycleChangedListener(this);
    	        jBack = new javax.swing.JButton();
    	        jtxtCustomer = new javax.swing.JTextField();
    	        btnCustomer = new javax.swing.JButton();
    	        jLabel3 = new javax.swing.JLabel();
    	        jTxtStartDate = new javax.swing.JTextField();
    	        btnDateStart = new javax.swing.JButton();
    	        jLabel4 = new javax.swing.JLabel();
    	        jTxtEndDate = new javax.swing.JTextField();
    	        btnDateEnd = new javax.swing.JButton();
    	        jButton1 = new javax.swing.JButton();
    	        
    	                jToolBar1.setRollover(true);
    	                
    	                        jBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1leftarrow.png"))); // NOI18N
    	                        jBack.setFocusable(false);
    	                        jBack.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                        jBack.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                        jBack.addActionListener(new java.awt.event.ActionListener() {
    	                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                jBackActionPerformed(evt);
    	                            }
    	                        });
    	                        jToolBar1.add(jBack);
    	                        
    	                                jtxtCustomer.setPreferredSize(new java.awt.Dimension(100, 25));
    	                                jToolBar1.add(jtxtCustomer);
    	                                
    	                                        btnCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/kuser.png"))); // NOI18N
    	                                        btnCustomer.setFocusPainted(false);
    	                                        btnCustomer.setFocusable(false);
    	                                        btnCustomer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                        btnCustomer.setMargin(null);
    	                                        btnCustomer.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                        btnCustomer.setRequestFocusEnabled(false);
    	                                        btnCustomer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                        btnCustomer.addActionListener(new java.awt.event.ActionListener() {
    	                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                btnCustomerActionPerformed(evt);
    	                                            }
    	                                        });
    	                                        jToolBar1.add(btnCustomer);
    	                                        
    	                                                jLabel3.setText(AppLocal.getIntString("Label.StartDate")); // NOI18N
    	                                                jToolBar1.add(jLabel3);
    	                                                
    	                                                        jTxtStartDate.setMinimumSize(new java.awt.Dimension(6, 15));
    	                                                        jTxtStartDate.setPreferredSize(new java.awt.Dimension(150, 22));
    	                                                        jToolBar1.add(jTxtStartDate);
    	                                                        
    	                                                                btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
    	                                                                btnDateStart.setFocusable(false);
    	                                                                btnDateStart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                btnDateStart.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                btnDateStart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                btnDateStart.addActionListener(new java.awt.event.ActionListener() {
    	                                                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                        btnDateStartActionPerformed(evt);
    	                                                                    }
    	                                                                });
    	                                                                jToolBar1.add(btnDateStart);
    	                                                                
    	                                                                        jLabel4.setText(AppLocal.getIntString("Label.EndDate")); // NOI18N
    	                                                                        jToolBar1.add(jLabel4);
    	                                                                        
    	                                                                                jTxtEndDate.setPreferredSize(new java.awt.Dimension(150, 22));
    	                                                                                jToolBar1.add(jTxtEndDate);
    	                                                                                
    	                                                                                        btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
    	                                                                                        btnDateEnd.setFocusable(false);
    	                                                                                        btnDateEnd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                                        btnDateEnd.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                                        btnDateEnd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                                        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
    	                                                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                                                btnDateEndActionPerformed(evt);
    	                                                                                            }
    	                                                                                        });
    	                                                                                        jToolBar1.add(btnDateEnd);
    	                                                                                                
    	                                                                                                        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png"))); // NOI18N
    	                                                                                                        jButton1.setFocusable(false);
    	                                                                                                        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                                                        jButton1.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                                                        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                                                        jButton1.addActionListener(new java.awt.event.ActionListener() {
    	                                                                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                                                                jButton1ActionPerformed(evt);
    	                                                                                                            }
    	                                                                                                        });
    	                                                                                                        jToolBar1.add(jButton1);
    	                                                                                                                
    	                                                                                                                btnNewButton_1 = new JButton("");
    	                                                                                                                btnNewButton_1.addActionListener(new ActionListener() {
    	                                                                                                                	public void actionPerformed(ActionEvent e) {
    	                                                                                                                		m_jResert1ActionPerformed(e);
    	                                                                                                                	}
    	                                                                                                                });
    	                                                                                                                btnNewButton_1.setIcon(new ImageIcon(JPassengerFlowAnalysisPanel.class.getResource("/com/openbravo/images/reload.png")));
    	                                                                                                                jToolBar1.add(btnNewButton_1);
    	
    	jNavigator = new JPanel();
    	jNavigator.setPreferredSize(new Dimension(240, 10));
    	add(jNavigator, BorderLayout.EAST);
    	jNavigator.setLayout(new CardLayout(0, 0));
    }// </editor-fold>//GEN-END:initComponents

	public JProductFinder getProductFinder() {
        if(productFinder==null)
            productFinder=JProductFinder.getInstance(this, dlSales,JProductFinder.PRODUCT_ALL);
        return productFinder;
    }

	private void btnCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomerActionPerformed
        JCustomerFinder finder = JCustomerFinder.getCustomerFinder(this, dlSales);
        finder.search(null);
        finder.setVisible(true);
       // try {
            customerInfo=finder.getSelectedCustomer();
            jtxtCustomer.setText(finder.getSelectedCustomer() == null
                    ? null
                    : finder.getSelectedCustomer().getName());// dlSales.loadCustomer(customerInfo.getId()).toString());
       // } catch (BasicException e) {
          //  MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotfindcustomer"), e);
          //  msg.show(this);
       // }
}//GEN-LAST:event_btnCustomerActionPerformed

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateEndActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            try {
				beforeFilterParas=(Object[]) createValue();
				reQuery();
			} catch (BasicException e) {
				new MessageInf(e).show(this);
			} 
}//GEN-LAST:event_jButton1ActionPerformed

    private void jBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBackActionPerformed
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        showPanel="table";
        jBack.setVisible(false);
    }//GEN-LAST:event_jBackActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCustomer;
    private javax.swing.JButton btnDateEnd;
    private javax.swing.JButton btnDateStart;
    private javax.swing.JButton jBack;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField jTxtEndDate;
    private javax.swing.JTextField jTxtStartDate;
    private javax.swing.JTextField jtxtCustomer;
    private JPanel panel;
    private JPanel jNavigator;
	private CardLayout cl1;
	private JProductCategoryNavigator productCategoryNavigator;
    // End of variables declaration//GEN-END:variables
	private JCustomerNavigator customerNavigator;
	private AppView app;
	private JOperationProductCategoryNavigator operationProductCategoryNavigator;
	private JButton btnNewButton_1;
	protected String productId;
	private JSubjectNavigator subjectNavigator;

    public String getTitle() {
        return "";
    }

    public void activate() throws BasicException {
        
        
    }

    public boolean deactivate() {
        return true;
    }

    public JComponent getComponent() {
        return this;
    }

    public void init(I_AppView app) throws BeanFactoryException {
    	this.app=(AppView) app;
        manager= new AnalysisModelManager(dlSales);
        view=new JPassengerFlowHistoryView(manager);
        jPanel1.add(view,"view");
        table=new JPassengerFlowTableView(this,manager);
        jPanel1.add(table,"table");
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        jBack.setVisible(false);
        showPanel="table";
        cl1=(CardLayout)(jNavigator.getLayout());
    }
    
    private void reQuery(){
        try{
        if(beforeFilterParas==null)
        	beforeFilterParas=(Object[]) createValue();
     	manager.query(beforeFilterParas);
        table.peferredJXTableColumn();
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        showPanel="table";
        jBack.setVisible(false);
      } catch (BasicException ex) {
         new MessageInf(ex).show(this);
     }
     }
    
    
    public Object getBean() {
        return this;
    }

	protected void m_jResert1ActionPerformed(ActionEvent e) {
		beforeFilterParas=null;
		jTxtStartDate.setText(null);
		jTxtEndDate.setText(null);
		if(!app.isCustomerView()){
			customerInfo = null;
			jtxtCustomer.setText(null);
		}
		timerFilter.resert();
	}
    
    
    public Object createValue() throws BasicException {
        Object[] afilter = new Object[6];
          // Date range
        Object startdate = Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

        afilter[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[1] = startdate;
        afilter[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[3] = enddate;

        //Customer
        if (customerInfo == null) {
            afilter[4] = QBFCompareEnum.COMP_NONE;
            afilter[5] = null;
        } else {
            afilter[4] = QBFCompareEnum.COMP_EQUALS;
            afilter[5] = customerInfo.getId();
        }   
       return afilter;
    }

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if(beforeFilterParas==null)
			try {
				beforeFilterParas=(Object[]) createValue();
			} catch (BasicException ex) {
				new MessageInf(ex).show(this);
				return;
			}
			Object startdate =e.getStartDate();
	        Object enddate = e.getEndDate();
	        
			beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
			beforeFilterParas[1] = startdate;
			beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
			beforeFilterParas[3] = enddate;	
			reQuery();
			jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(startdate));
		    jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(enddate));
	}

	public void showDialy() {
		CardLayout cl = (CardLayout)(jPanel1.getLayout());
		cl.show(jPanel1, "view");
		showPanel="view";
	    jBack.setVisible(true);
	}

	@Override
	public String getModelName() {
		return ERP_UI_Swing_Model.MODEL_NAME;
	}

	@Override
	public void setModel(ERP_UI_Swing_Model mode) {
		dlSales=mode.getDLERP();
        dlCustomers=mode.getDLCustomer();
        dlSystem=mode.getDLSystem();
		
	}
}

